<div class="fsh-rightPanel">
    <label> <input type="password" style="display: none"></label>
    <input type="hidden" id="class_id" value="<%=squad_id%>"/>
    <div class="layui-anim layui-anim-upbit">
        <div class="layui-form-item wx-search" id="list_form">
            <div class="layui-inline">
                <input type="text" name="nickname" placeholder="请输入用户名称" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn btn-primary" id="search">搜索</button>
        </div>
        <table id="table" class="layui-hide" lay-filter="studentList"></table>
    </div>
</div>
<script type="text/html" id="toolBar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add_student">+添加学生</button>
    </div>
</script>
<script>
    var classId = $('#class_id').val();
    layui.use(['layer', 'table'], function () {
        var layer = layui.layer;
        var table = layui.table;
        var tableObj2 = table.render({
            elem: '#table',
            url: context + '/api/student/getSquad/' + classId,
            //开启头部工具栏，并为其绑定左侧模板，
            toolbar: '#toolBar',
            defaultToolbar: [],
            request: {
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            align: 'center'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'},
                {
                    field: 'photo', title: '头像', align: 'center', templet: function (d) {
                        return d.photo !== undefined ? '<div><img src="' + d.photo + '" style="height:100px;width:100px;" alt=""/></div>' : '';
                    }
                },
                {field: 'nickname', title: '姓名', align: 'center'},
                {
                    field: 'sex', title: '性别', align: 'center', templet: function (d) {
                        return d.sex === 0 ? '男' : '女';
                    }
                },
                {field: 'email', title: '邮箱', align: 'center'},
                {field: 'createDate', title: '创建时间', align: 'center'},
                {
                    field: 'lock', title: '操作', width: 240, align: 'center', unresize: true,
                    templet: function (d) {
                        return '<a href="javascript:;" class="font-primary" lay-event="del">删除</a>';
                    }
                }
            ]]
            , page: true

        });

        // 检索
        $("#search").click(function () {
            reloadTable(tableObj2);
        });

        //头工具栏事件
        table.on('toolbar(studentList)', function (obj) {
            var layEvent = obj.event; //获得 lay-event 对应的值
            // 添加
            if (layEvent === 'add_student') {
                showDialog({
                    title: '添加学生'
                    , template: 'add_student'
                    , width: 800
                    , height: 600
                    , yes: function (index, layero) {
                        var checkData = table.checkStatus('table2').data;
                        layer.close(index);
                        var data = {};
                        for (var i = 0; i < checkData.length; i++) {
                            data.user_id = checkData[i].user_id;
                            data.squad_id = classId;
                            $.ajax({
                                url: context + '/api/student/updateSquad',
                                type: 'PATCH',
                                async: false,
                                dataType: 'json',
                                contentType: "application/json;charset-UTF-8",
                                data: JSON.stringify(data),
                                success: function (result) {
                                    if (result.success) {
                                        layer.msg("添加成功");
                                        reloadTable(tableObj2);
                                    } else {
                                        layer.msg(result.msg);
                                    }
                                },
                                error: function () {
                                    layer.msg("添加异常");
                                }
                            })
                        }
                    }

                })
            }
        });
        //表格内部操作按钮监听
        table.on('tool(studentList)', function (obj) { //注：tool是工具条事件名，mainList是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            // 删除
            if (layEvent === 'del') {
                layer.confirm('确认移除此学生？', {
                    icon: 7,
                    title: "提示",
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    var json = {
                        user_id: data.user_id,
                        squad_id: 0
                    };
                    $.ajax({
                        url: context + '/api/student/updateSquad',
                        type: 'PATCH',
                        async: false,
                        dataType: 'json',
                        contentType: "application/json;charset-UTF-8",
                        data: JSON.stringify(json),
                        success: function (result) {
                            if (result.success) {
                                layer.msg("移除成功");
                                reloadTable(tableObj2);
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error: function () {
                            layer.msg("移除异常");
                        }
                    })
                }, function () {
                    layer.closeAll();
                });
            }
        });

    });
</script>
